
import { _decorator, Component, Node, Sprite, UITransform, Animation, AnimationClip, animation, SpriteFrame } from 'cc';
import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM } from '../Enum';

import { IEntity } from '../Levels';
import { EntityManager } from './EntityManager';
import EventManager from '../Runtime/EventManager';
import DataManager from '../Runtime/DataManager';
import { PlayerManager } from '../Script/Player/PlayerManager';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = PlayerManager
 * DateTime = Tue Jun 27 2023 13:04:45 GMT+0800 (中国标准时间)
 * Author = zshzshzsh15
 * FileBasename = PlayerManager.ts
 * FileBasenameNoExtension = PlayerManager
 * URL = db://assets/Script/Player/PlayerManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */


const ABIMATION_SPEED = 1 / 8  //帧数  8帧
@ccclass('EnemyManager')
export class EnemyManager extends EntityManager {




    async init(params:IEntity) {
        // //拆的代码
        // const sprite = this.addComponent(Sprite)
        // sprite.sizeMode=Sprite.SizeMode.CUSTOM  //能自定义大小

        // const ui=this.getComponent(UITransform)
        // ui.setContentSize(TILE_WIDTH*4,TILE_HEIGHT*4)



        // //控制turnleft组件
        // this.fsm = this.addComponent(WoodenSkeletonStateMachine)
        // await this.fsm.init() //初始化状态机


        super.init(params)

        EventManager.Instance.on(EVENT_ENUM.PLAYER_BORN, this.onChangeDirection, this)
        EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END, this.onChangeDirection, this)

        EventManager.Instance.on(EVENT_ENUM.ATTACK_ENMEY, this.onDead, this)



        //敌人先生成，然后朝向玩家
        this.onChangeDirection(true)

        // this.targetX = this.x
        // this.targetY = this.y

        // // //设置初始方向
        // // this.direction=DIRECTION_ENUM.TOP
        // // //设置当前人物状态
        // // this.state=ENTITY_STATE_ENUM.IDLE


        // //await this.render()
        // EventManager.Instance.on(EVENT_ENUM.PLAYER_CTRL, this.inputHandle, this)







    }

    onDestroy() {
        super.onDestroy()
        EventManager.Instance.off(EVENT_ENUM.PLAYER_BORN, this.onChangeDirection)
        EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END, this.onChangeDirection)

        EventManager.Instance.off(EVENT_ENUM.ATTACK_ENMEY, this.onDead)
    }

    onDead(id: string) {
        if (this.state === ENTITY_STATE_ENUM.DEATH) {
            return
        }
        if (this.id === id) {
            this.state = ENTITY_STATE_ENUM.DEATH
            
        }


    }

    //&& playeState !==ENTITY_STATE_ENUM.DEATH && playeState !== ENTITY_STATE_ENUM.AIRDEATH

    onChangeDirection(isInit: boolean = false) {

        //判断玩家有没有
        if (this.state === ENTITY_STATE_ENUM.DEATH || !DataManager.Instance.player) {
            return
        }
        const { x: playerx, y: playery } = DataManager.Instance.player //拿到玩家的信息

        const disX = Math.abs(this.x - playerx)
        const disY = Math.abs(this.y - playery)

        if (disX === disY && !isInit) {
            return
        }

        if (playerx >= this.x && playery <= this.y) {//第一
            if (disY > disX) {
                this.direction = DIRECTION_ENUM.TOP
            } else {
                this.direction = DIRECTION_ENUM.RIGHT
            }

        } else if (playerx <= this.x && playery <= this.y) { //第二象限
            if (disY > disX) {
                this.direction = DIRECTION_ENUM.TOP
            } else {
                this.direction = DIRECTION_ENUM.LEFT
            }
        } else if (playerx <= this.x && playery >= this.y) {
            if (disY > disX) {
                this.direction = DIRECTION_ENUM.BOTTOM
            } else {
                this.direction = DIRECTION_ENUM.LEFT
            }
        } else if (playerx >= this.x && playery >= this.y) {
            if (disY > disX) {
                this.direction = DIRECTION_ENUM.BOTTOM
            } else {
                this.direction = DIRECTION_ENUM.RIGHT
            }
        }

    }


    //渲染
    // async render(){
    //     const sprite = this.addComponent(Sprite)
    //     sprite.sizeMode=Sprite.SizeMode.CUSTOM  //能自定义大小

    //     const ui=this.getComponent(UITransform)
    //     ui.setContentSize(TILE_WIDTH*4,TILE_HEIGHT*4)


    //     //加载资源
    //     const spriteFrames=await ResourceManager.Instance.loadDir("texture/player/idle/top")

    //     const animationsComponent=this.addComponent(Animation)

    //     //甚至clip
    //     const animationClip = new AnimationClip()


    //     //轨道
    //     const track =new animation.ObjectTrack()//创建对象轨道
    //     track.path = new animation.TrackPath().toComponent(Sprite).toProperty('spriteFrame'); // 指定轨道路径，即指定目标对象为 "Foo" 子节点的 "position" 属性


    //     //处理资源数组
    //     const frams :   Array<[number,SpriteFrame]>=spriteFrames.map((item,index)=>[ABIMATION_SPEED*index,item]) //得到关键帧列表
    //     track.channel.curve.assignSorted(
    //         frams
    // //         [ // 为 x 通道的曲线添加关键帧
    // //     [0.4, ({ value: 0.4 })],
    // //     [0.6, ({ value: 0.6 })],
    // //     [0.8, ({ value: 0.8 })],
    // // ]
    // )

    //     //最后将轨道添加到动画剪辑
    //     animationClip.addTrack(track)

    //     animationClip.wrapMode=AnimationClip.WrapMode.Loop

    //     //
    //     animationClip.duration=frams.length*ABIMATION_SPEED //整个动画剪辑的周期
    //     animationsComponent.defaultClip=animationClip
    //     animationsComponent.play()
    // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
